package com.accloud.cloudservice;

import android.os.Handler;
import android.os.HandlerThread;
import com.accloud.cloudservice.AC;
import com.accloud.common.ACConstant;
import com.accloud.service.ACDeviceFind;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACDeviceSecurityMode;
import com.accloud.service.ACException;
import com.accloud.service.ACWifiInfo;
import com.accloud.service.Receiver;
import com.accloud.utils.ACUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.java_websocket.drafts.Draft_75;

public class ACDeviceLocalManager {
    static final int DEFAULT_INTERVAL_MS = 500;
    static final int DEFAULT_TIMEOUT_MS = 2000;
    static final long DEVICE_PUSH_INTERVAL_MS = 2000;
    static final int DEVICE_REPORT_DURATION_MS = ((int) TimeUnit.SECONDS.toMillis(60));
    static final int DEVICE__BC_PLAIN_RECEIVER = 7689;
    private static final int DEVICE__UC_SECURE_RECEIVER = 6689;
    private static final String HOST_BROADCAST = "255.255.255.255";
    static final int MOBILE__BC_PLAIN_RECEIVER = 8689;
    private static final int MOBILE__BC_SECURE_RECEIVER = 8690;
    static final int MOBILE__STATUS_RECEIVER = 6691;
    private static final int MOBILE__UC_SECURE_RECEIVER = 6690;
    private static final int PCT_STATE_CONNECT_CLOUD = 6;
    static final int REQUEST_DEVICE_STATUS = 3;
    static final int REQUEST_FIND_DEVICE = 30;
    private static final int REQUEST_GET_WIFI = 1;
    private static final int REQUEST_SET_WIFI = 2;
    private static final int REQUEST_SUBSCRIBE_DEVICE = 41;
    @Deprecated
    static final int RESPONSE_DEVICE_LINK = 27;
    static final int RESPONSE_FIND_DEVICE = 31;
    private static final long SUBSCRIBE_INTERVAL_MS = 5000;
    public static final String TAG = ACDeviceLocalManager.class.getSimpleName();
    private AbleFind ableFind;
    private List<AC.LocalDataReceiver> dataReceivers = new ArrayList();
    private List<AC.LocalDeviceObserver> deviceObservers = new ArrayList();
    private List<LocalDeviceWatcher> deviceWatchers = new ArrayList();
    private final Handler handler;
    private final Handler receiveDataHandler;
    private Runnable receiveDataTask = new Runnable() {
        /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass9 */

        /* JADX WARNING: Removed duplicated region for block: B:26:0x00ea  */
        /* JADX WARNING: Removed duplicated region for block: B:32:0x00f9  */
        /* JADX WARNING: Removed duplicated region for block: B:48:? A[RETURN, SYNTHETIC] */
        @Override // java.lang.Runnable
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void run() {
            /*
            // Method dump skipped, instructions count: 268
            */
            throw new UnsupportedOperationException("Method not decompiled: com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass9.run():void");
        }
    };
    private Runnable subscribeTask = new Runnable() {
        /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass10 */

        /* JADX WARNING: Removed duplicated region for block: B:11:0x0071  */
        @Override // java.lang.Runnable
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void run() {
            /*
                r10 = this;
                com.accloud.cloudservice.ACDeviceLocalManager r5 = com.accloud.cloudservice.ACDeviceLocalManager.this
                android.os.Handler r5 = com.accloud.cloudservice.ACDeviceLocalManager.access$500(r5)
                r5.removeCallbacks(r10)
                com.accloud.cloudservice.ACDeviceLocalManager r5 = com.accloud.cloudservice.ACDeviceLocalManager.this
                boolean r5 = com.accloud.cloudservice.ACDeviceLocalManager.access$200(r5)
                if (r5 != 0) goto L_0x0012
            L_0x0011:
                return
            L_0x0012:
                r3 = 0
                r5 = 8
                java.nio.ByteBuffer r5 = java.nio.ByteBuffer.allocate(r5)     // Catch:{ IOException -> 0x006e }
                long r6 = com.accloud.cloudservice.AC.majorDomainId     // Catch:{ IOException -> 0x006e }
                int r6 = (int) r6     // Catch:{ IOException -> 0x006e }
                short r6 = (short) r6     // Catch:{ IOException -> 0x006e }
                java.nio.ByteBuffer r5 = r5.putShort(r6)     // Catch:{ IOException -> 0x006e }
                r6 = 2
                java.nio.ByteBuffer r5 = r5.putShort(r6)     // Catch:{ IOException -> 0x006e }
                java.util.concurrent.TimeUnit r6 = java.util.concurrent.TimeUnit.MILLISECONDS     // Catch:{ IOException -> 0x006e }
                int r7 = com.accloud.cloudservice.ACDeviceLocalManager.DEVICE_REPORT_DURATION_MS     // Catch:{ IOException -> 0x006e }
                long r8 = (long) r7     // Catch:{ IOException -> 0x006e }
                long r6 = r6.toSeconds(r8)     // Catch:{ IOException -> 0x006e }
                int r6 = (int) r6     // Catch:{ IOException -> 0x006e }
                short r6 = (short) r6     // Catch:{ IOException -> 0x006e }
                java.nio.ByteBuffer r5 = r5.putShort(r6)     // Catch:{ IOException -> 0x006e }
                java.util.concurrent.TimeUnit r6 = java.util.concurrent.TimeUnit.MILLISECONDS     // Catch:{ IOException -> 0x006e }
                r8 = 2000(0x7d0, double:9.88E-321)
                long r6 = r6.toSeconds(r8)     // Catch:{ IOException -> 0x006e }
                int r6 = (int) r6     // Catch:{ IOException -> 0x006e }
                short r6 = (short) r6     // Catch:{ IOException -> 0x006e }
                java.nio.ByteBuffer r5 = r5.putShort(r6)     // Catch:{ IOException -> 0x006e }
                byte[] r2 = r5.array()     // Catch:{ IOException -> 0x006e }
                com.accloud.cloudservice.LocalMessage r5 = new com.accloud.cloudservice.LocalMessage     // Catch:{ IOException -> 0x006e }
                r6 = 41
                r5.<init>(r6, r2)     // Catch:{ IOException -> 0x006e }
                byte[] r5 = r5.toBytes()     // Catch:{ IOException -> 0x006e }
                r6 = 7689(0x1e09, float:1.0775E-41)
                java.net.DatagramPacket r1 = com.accloud.utils.LocalUtils.formatBroadcast(r5, r6)     // Catch:{ IOException -> 0x006e }
                java.net.DatagramSocket r4 = new java.net.DatagramSocket     // Catch:{ IOException -> 0x006e }
                r5 = 0
                r4.<init>(r5)     // Catch:{ IOException -> 0x006e }
                r4.send(r1)     // Catch:{ IOException -> 0x0075 }
                r3 = r4
            L_0x0062:
                com.accloud.cloudservice.ACDeviceLocalManager r5 = com.accloud.cloudservice.ACDeviceLocalManager.this
                android.os.Handler r5 = com.accloud.cloudservice.ACDeviceLocalManager.access$500(r5)
                r6 = 5000(0x1388, double:2.4703E-320)
                r5.postDelayed(r10, r6)
                goto L_0x0011
            L_0x006e:
                r0 = move-exception
            L_0x006f:
                if (r3 == 0) goto L_0x0062
                r3.close()
                goto L_0x0062
            L_0x0075:
                r0 = move-exception
                r3 = r4
                goto L_0x006f
            */
            throw new UnsupportedOperationException("Method not decompiled: com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass10.run():void");
        }
    };
    private boolean subscribed;
    private Runnable watchTask = new Runnable() {
        /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass11 */

        @Override // java.lang.Runnable
        public void run() {
            ACDeviceLocalManager.this.handler.removeCallbacks(this);
            if (ACDeviceLocalManager.this.subscribed) {
                boolean renew = false;
                List<LocalDeviceWatcher> missing = new ArrayList<>();
                for (LocalDeviceWatcher watcher : ACDeviceLocalManager.this.deviceWatchers) {
                    long missingTime = System.currentTimeMillis() - watcher.lastRecord;
                    if (missingTime > 10000) {
                        missing.add(watcher);
                    } else if (missingTime > ACDeviceLocalManager.SUBSCRIBE_INTERVAL_MS) {
                        renew = true;
                    }
                }
                if (missing.size() > 0) {
                    ACDeviceLocalManager.this.removeDevices(missing);
                }
                if (renew) {
                    ACDeviceLocalManager.this.handler.post(ACDeviceLocalManager.this.subscribeTask);
                }
                ACDeviceLocalManager.this.handler.postDelayed(this, ACDeviceLocalManager.SUBSCRIBE_INTERVAL_MS);
            }
        }
    };

    ACDeviceLocalManager() {
        HandlerThread thread = new HandlerThread(TAG);
        thread.start();
        this.handler = new Handler(thread.getLooper());
        HandlerThread receiveLocalData = new HandlerThread("ReceiveLocalData");
        receiveLocalData.start();
        this.receiveDataHandler = new Handler(receiveLocalData.getLooper());
    }

    /* access modifiers changed from: package-private */
    public void sendToDevice(String physicalDeviceId, ACDeviceMsg deviceMsg, int timeout, PayloadCallback<ACDeviceMsg> callback) {
        ACDeviceSecurityMode mode;
        final PayloadEventCallback<ACDeviceMsg> callbackWrapper = new PayloadEventCallback<>(new ControlEvent(ControlEvent.LOCAL, null, physicalDeviceId, deviceMsg), callback);
        if (!ACUtils.isWifiConnected()) {
            callbackWrapper.error(new ACException(ACException.NO_WIFI_CONNECTED, "no wifi connected"));
            return;
        }
        LocalMessage localMessage = new LocalMessage(deviceMsg.getCode(), deviceMsg.getContent());
        for (ACDeviceFind deviceFind : ACConstant.deviceFinds) {
            if (deviceFind.getPhysicalDeviceId().equals(physicalDeviceId)) {
                try {
                    if (deviceFind.getSecurityMode() == -1) {
                        mode = deviceMsg.getSecurityMode();
                    } else {
                        mode = ACDeviceSecurityMode.values()[deviceFind.getSecurityMode()];
                    }
                    send(deviceFind, ((ACBindManager) AC.bindMgr()).getAESKey(deviceFind.getPhysicalDeviceId(), mode), localMessage, timeout, new PayloadCallback<LocalMessage>() {
                        /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass1 */

                        public void success(LocalMessage localMessage) {
                            callbackWrapper.success(new ACDeviceMsg(localMessage.msgCode, localMessage.payload));
                        }

                        @Override // com.accloud.cloudservice.BaseCallback
                        public void error(ACException e) {
                            callbackWrapper.error(e);
                        }
                    });
                    return;
                } catch (ACException e) {
                    callback.error(e);
                    return;
                }
            }
        }
        callbackWrapper.error(new ACException(ACException.ENTRY_EMPTY, "physicalDeviceId[" + physicalDeviceId + "] not found on local network"));
    }

    private void send(final ACDeviceFind deviceFind, final String aesKey, final LocalMessage localMessage, final int timeout, final PayloadCallback<LocalMessage> callback) {
        new Thread() {
            /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass2 */

            @Override // java.lang.Thread, java.lang.Runnable
            public void run() {
                switch (deviceFind.getLanMode()) {
                    case 0:
                        ACDeviceLocalManager.this.sendDeviceByTcp(deviceFind.getIp(), localMessage, aesKey, timeout, callback);
                        return;
                    case 1:
                        ACDeviceLocalManager.this.sendDeviceByUdp(deviceFind.getIp(), localMessage, aesKey, timeout, ACDeviceLocalManager.MOBILE__UC_SECURE_RECEIVER, callback);
                        return;
                    case 2:
                        ACDeviceLocalManager.this.sendDeviceByHttp(deviceFind.getIp(), localMessage, aesKey, timeout, callback);
                        return;
                    default:
                        return;
                }
            }
        }.start();
    }

    /* access modifiers changed from: package-private */
    public void findDevice(int timeout_ms, int interval_ms, final PayloadCallback<List<ACDeviceFind>> callback) {
        if (timeout_ms < interval_ms * 2) {
            throw new IllegalArgumentException("Timeout should be twice bigger than interval");
        }
        stopFind();
        final Set<ACDeviceFind> deviceSet = new HashSet<>();
        this.ableFind = new AbleFind(timeout_ms, interval_ms, new Receiver<ACDeviceFind>() {
            /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass3 */

            public void onReceive(ACDeviceFind localDevice) {
                deviceSet.add(localDevice);
            }
        });
        this.ableFind.execute(new VoidCallback() {
            /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass4 */

            @Override // com.accloud.cloudservice.VoidCallback
            public void success() {
                ACDeviceLocalManager.this.stopFind();
                ACConstant.deviceFinds.clear();
                ACConstant.deviceFinds.addAll(deviceSet);
                callback.success(new ArrayList(deviceSet));
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException error) {
                ACDeviceLocalManager.this.stopFind();
                ACConstant.deviceFinds.clear();
                ACConstant.deviceFinds.addAll(deviceSet);
                callback.success(new ArrayList(deviceSet));
            }
        });
    }

    /* access modifiers changed from: package-private */
    public void findLinkingDevice(int timeout_ms, int interval_ms, final PayloadCallback<ACDeviceFind> callback) {
        stopFind();
        this.ableFind = new AbleLinkingFind(timeout_ms, interval_ms, new Receiver<ACDeviceFind>() {
            /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass5 */

            public void onReceive(ACDeviceFind localDevice) {
                ACDeviceLocalManager.this.stopFind();
                callback.success(localDevice);
            }
        });
        this.ableFind.execute(new VoidCallback() {
            /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass6 */

            @Override // com.accloud.cloudservice.VoidCallback
            public void success() {
                ACDeviceLocalManager.this.stopFind();
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException error) {
                ACDeviceLocalManager.this.stopFind();
                callback.error(error);
            }
        });
    }

    /* access modifiers changed from: package-private */
    public void stopFind() {
        if (this.ableFind != null) {
            this.ableFind.cancel();
        }
    }

    public void getWifiFromAP(final int timeout_ms, final PayloadCallback<List<ACWifiInfo>> callback) {
        new Thread() {
            /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass7 */

            @Override // java.lang.Thread, java.lang.Runnable
            public void run() {
                ACDeviceLocalManager.this.sendDeviceByUdp("255.255.255.255", new LocalMessage(1, null), null, timeout_ms, ACDeviceLocalManager.MOBILE__UC_SECURE_RECEIVER, new PayloadCallback<LocalMessage>() {
                    /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass7.AnonymousClass1 */

                    public void success(LocalMessage localMessage) {
                        if (localMessage.msgCode != 1) {
                            callback.error(new ACException(ACException.INTERNAL_ERROR, "Unknown response from AP"));
                            return;
                        }
                        ByteBuffer buffer = ByteBuffer.wrap(localMessage.payload);
                        int count = buffer.get() & Draft_75.END_OF_FRAME;
                        ArrayList arrayList = new ArrayList();
                        for (int i = 0; i < count; i++) {
                            int ssidLen = buffer.get() & Draft_75.END_OF_FRAME;
                            int power = buffer.get() & Draft_75.END_OF_FRAME;
                            byte[] bssid = new byte[ssidLen];
                            buffer.get(bssid);
                            arrayList.add(new ACWifiInfo(new String(bssid).trim(), power));
                        }
                        callback.success(arrayList);
                    }

                    @Override // com.accloud.cloudservice.BaseCallback
                    public void error(ACException error) {
                        callback.error(error);
                    }
                });
            }
        }.start();
    }

    public void setWifiToAP(String ssid, String password, final int timeout_ms, final VoidCallback callback) {
        final byte[] bytes = new byte[64];
        byte[] s = ssid.getBytes();
        byte[] p = password.getBytes();
        System.arraycopy(s, 0, bytes, 0, s.length);
        System.arraycopy(p, 0, bytes, 32, p.length);
        new Thread() {
            /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass8 */

            @Override // java.lang.Thread, java.lang.Runnable
            public void run() {
                ACDeviceLocalManager.this.sendDeviceByUdp("255.255.255.255", new LocalMessage(2, bytes), null, timeout_ms, ACDeviceLocalManager.MOBILE__UC_SECURE_RECEIVER, new PayloadCallback<LocalMessage>() {
                    /* class com.accloud.cloudservice.ACDeviceLocalManager.AnonymousClass8.AnonymousClass1 */

                    public void success(LocalMessage localMessage) {
                        byte[] content = localMessage.payload;
                        if (localMessage.msgCode == 2 && content.length >= 4 && content[0] == 0) {
                            callback.success();
                        } else {
                            callback.error(new ACException(ACException.INTERNAL_ERROR, "Fail to config device AP"));
                        }
                    }

                    @Override // com.accloud.cloudservice.BaseCallback
                    public void error(ACException error) {
                        callback.error(error);
                    }
                });
            }
        }.start();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void sendDeviceByTcp(String ipAddress, LocalMessage message, String aesKey, int timeoutMs, PayloadCallback<LocalMessage> callback) {
        Exception e;
        Socket socket = new Socket();
        try {
            SecuritySession session = new SecuritySession(aesKey);
            socket.connect(new InetSocketAddress(ipAddress, 9689), timeoutMs);
            socket.setSoTimeout(timeoutMs);
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(session.encrypt(message));
            outputStream.flush();
            callback.success(session.decrypt(socket.getInputStream()));
            try {
                socket.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        } catch (IOException e3) {
            e = e3;
            try {
                try {
                    callback.error(new ACException(ACException.INTERNAL_ERROR, e));
                } catch (Exception e4) {
                    e4.printStackTrace();
                }
            } finally {
                try {
                    socket.close();
                } catch (IOException e5) {
                    e5.printStackTrace();
                }
            }
        } catch (GeneralSecurityException e6) {
            e = e6;
            callback.error(new ACException(ACException.INTERNAL_ERROR, e));
        } catch (ACException error) {
            callback.error(error);
            try {
                socket.close();
            } catch (IOException e7) {
                e7.printStackTrace();
            }
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:13:0x00ac, code lost:
        r25 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:14:0x00ad, code lost:
        r18 = r19;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:19:0x00c5, code lost:
        r18.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:44:0x01a5, code lost:
        r12 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:45:0x01a6, code lost:
        r12.printStackTrace();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:53:0x01d0, code lost:
        r25 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:54:0x01d1, code lost:
        r18 = r19;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:56:0x01d5, code lost:
        r18.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:80:?, code lost:
        return;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:81:?, code lost:
        return;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:13:0x00ac A[ExcHandler: IOException (e java.io.IOException), Splitter:B:8:0x0041] */
    /* JADX WARNING: Removed duplicated region for block: B:19:0x00c5  */
    /* JADX WARNING: Removed duplicated region for block: B:53:0x01d0 A[ExcHandler: all (th java.lang.Throwable), Splitter:B:8:0x0041] */
    /* JADX WARNING: Removed duplicated region for block: B:56:0x01d5  */
    /* JADX WARNING: Removed duplicated region for block: B:80:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void getLocalDeviceWifiLinkQuality(java.util.List<com.accloud.service.ACDeviceFind> r31, int r32, com.accloud.cloudservice.PayloadCallback<java.util.List<com.accloud.service.ACDeviceFind>> r33) {
        /*
        // Method dump skipped, instructions count: 501
        */
        throw new UnsupportedOperationException("Method not decompiled: com.accloud.cloudservice.ACDeviceLocalManager.getLocalDeviceWifiLinkQuality(java.util.List, int, com.accloud.cloudservice.PayloadCallback):void");
    }

    /* access modifiers changed from: package-private */
    /* JADX WARNING: Code restructure failed: missing block: B:20:0x00b3, code lost:
        r15 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:21:0x00b4, code lost:
        r12 = r13;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:26:0x00c6, code lost:
        r12.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:32:0x00e6, code lost:
        r12.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:36:0x00ed, code lost:
        r15 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:37:0x00ee, code lost:
        r12 = r13;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:39:0x00f2, code lost:
        r15 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:40:0x00f3, code lost:
        r12 = r13;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:51:?, code lost:
        return;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:52:?, code lost:
        return;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:20:0x00b3 A[ExcHandler: IOException (e java.io.IOException), Splitter:B:4:0x000f] */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x00c6  */
    /* JADX WARNING: Removed duplicated region for block: B:32:0x00e6  */
    /* JADX WARNING: Removed duplicated region for block: B:36:0x00ed A[ExcHandler: all (th java.lang.Throwable), Splitter:B:4:0x000f] */
    /* JADX WARNING: Removed duplicated region for block: B:51:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void sendDeviceByUdp(java.lang.String r23, com.accloud.cloudservice.LocalMessage r24, java.lang.String r25, int r26, int r27, com.accloud.cloudservice.PayloadCallback<com.accloud.cloudservice.LocalMessage> r28) {
        /*
        // Method dump skipped, instructions count: 253
        */
        throw new UnsupportedOperationException("Method not decompiled: com.accloud.cloudservice.ACDeviceLocalManager.sendDeviceByUdp(java.lang.String, com.accloud.cloudservice.LocalMessage, java.lang.String, int, int, com.accloud.cloudservice.PayloadCallback):void");
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void sendDeviceByHttp(String ipAddress, LocalMessage message, String aesKey, int timeout_ms, PayloadCallback<LocalMessage> callback) {
        try {
            HttpSecuritySession session = new HttpSecuritySession(aesKey);
            byte[] bytes = session.encrypt(message);
            HttpURLConnection connection = (HttpURLConnection) new URL("http://" + ipAddress + ":9200/appGetLanData").openConnection();
            connection.setConnectTimeout(timeout_ms);
            connection.setReadTimeout(timeout_ms);
            connection.setDoOutput(true);
            connection.connect();
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
            int responseCode = connection.getResponseCode();
            if (responseCode != 200) {
                callback.error(new ACException(responseCode, "Http error"));
                return;
            }
            byte[] result = new byte[connection.getContentLength()];
            InputStream inputStream = connection.getInputStream();
            inputStream.read(result);
            inputStream.close();
            callback.success(session.decrypt(result));
        } catch (ACException error) {
            callback.error(error);
        } catch (IOException | GeneralSecurityException e) {
            callback.error(new ACException(ACException.INTERNAL_ERROR, e));
        }
    }

    public void registerLocalDataReceiver(AC.LocalDataReceiver receiver) {
        this.dataReceivers.add(receiver);
        if (!this.subscribed) {
            this.subscribed = true;
            this.receiveDataHandler.post(this.receiveDataTask);
            this.handler.post(this.subscribeTask);
            this.handler.post(this.watchTask);
        }
    }

    public void unregisterLocalDataReceiver(AC.LocalDataReceiver receiver) {
        this.dataReceivers.remove(receiver);
        if (this.dataReceivers.size() == 0) {
            this.subscribed = false;
            this.deviceWatchers.clear();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void removeDevices(List<LocalDeviceWatcher> missing) {
        for (LocalDeviceWatcher watcher : missing) {
            this.deviceWatchers.remove(watcher);
            int i = 0;
            while (true) {
                if (i >= ACConstant.deviceFinds.size()) {
                    break;
                } else if (ACConstant.deviceFinds.get(i).getPhysicalDeviceId().equals(watcher.device.getPhysicalDeviceId())) {
                    ACConstant.deviceFinds.remove(i);
                    break;
                } else {
                    i++;
                }
            }
            for (AC.LocalDeviceObserver observer : this.deviceObservers) {
                observer.onDeviceChanged(watcher.device, false);
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void updateDevices(ACDeviceFind localDevice) {
        boolean found = false;
        for (ACDeviceFind device : ACConstant.deviceFinds) {
            if (device.getPhysicalDeviceId().equals(localDevice.getPhysicalDeviceId())) {
                device.setIp(localDevice.getIp());
                found = true;
            }
        }
        if (!found) {
            ACConstant.deviceFinds.add(localDevice);
        }
        if (this.subscribed) {
            int size = this.deviceWatchers.size();
            for (int i = 0; i < size; i++) {
                LocalDeviceWatcher watcher = this.deviceWatchers.get(i);
                if (watcher.device.getPhysicalDeviceId().equals(localDevice.getPhysicalDeviceId())) {
                    watcher.update(localDevice);
                    return;
                }
            }
            this.deviceWatchers.add(new LocalDeviceWatcher(localDevice));
            for (AC.LocalDeviceObserver observer : this.deviceObservers) {
                observer.onDeviceChanged(localDevice, true);
            }
        }
    }

    public void registerLocalDeviceObserver(AC.LocalDeviceObserver observer) {
        this.deviceObservers.add(observer);
    }

    public void unregisterLocalDeviceObserver(AC.LocalDeviceObserver observer) {
        this.deviceObservers.remove(observer);
    }

    /* access modifiers changed from: private */
    public class LocalDeviceWatcher {
        public ACDeviceFind device;
        private long lastRecord;

        public LocalDeviceWatcher(ACDeviceFind device2) {
            update(device2);
        }

        public void update(ACDeviceFind localDevice) {
            this.device = localDevice;
            this.lastRecord = System.currentTimeMillis();
        }
    }
}
